<template>
  <div class="ModifyPassword">
    <div class="header" style="font-size: 18px">
      <div class="line"></div>
      修改密码
    </div>
    <div style="display: flex; align-items: center; justify-content: center">
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="60px"
        class="demo-ruleForm"
      >
        <el-form-item label="旧密码" prop="oldPass">
          <el-input v-model="ruleForm.oldPass" autocomplete="off" />
        </el-form-item>
        <el-form-item label="新密码" prop="newPass">
          <el-input
            v-model="ruleForm.newPass"
            type="password"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input v-model="ruleForm.checkPass" type="password" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)" class="btn"
            >保存</el-button
          >
          <el-button @click="Return" class="btn1">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import api from "@/api";
import { useRouter, RouteLocationRaw } from "vue-router";
import { ElMessage, type FormInstance, type FormRules } from "element-plus";
const router = useRouter();
//绑定表单
const ruleFormRef = ref<FormInstance>();
//表单内容
const ruleForm = reactive({
  oldPass: "",
  checkPass: "",
  newPass: "",
});
//判断两次密码是否一致
const validatePass = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("必须填写"));
  } else {
    if (ruleForm.checkPass !== "") {
      if (!ruleFormRef.value) return;
      ruleFormRef.value.validateField("checkPass", () => null);
    }
    callback();
  }
};
const validatePass2 = (rule: any, value: any, callback: any) => {
  if (value === "") {
    callback(new Error("必须填写"));
  } else if (value !== ruleForm.newPass) {
    callback(new Error("二次密码不一致"));
  } else {
    callback();
  }
};
//验证规则
const rules = reactive<FormRules<typeof ruleForm>>({
  oldPass: [{ required: true, message: "请填写旧密码", trigger: "blur" }],
  newPass: [{ validator: validatePass, trigger: "blur" }],
  checkPass: [{ validator: validatePass2, trigger: "blur" }],
});
//提交
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate(async (valid) => {
    if (valid) {
      let result = await api.user.updatePasswordApi({
        id: localStorage.getItem("user_id"),
        password: ruleForm.checkPass,
        oldpassword: ruleForm.oldPass,
      });
      console.log(result);
      if (result.data.code == 200) {
        ElMessage.success("修改成功,即将返回登录页面");
        localStorage.removeItem("token");
        localStorage.removeItem("user_id");
        localStorage.removeItem("my_user");
        localStorage.removeItem("index");
        setTimeout(() => {
          router.push("/login");
        }, 3000);
      } else {
        ElMessage.warning("原密码错误，请重新输入");
        formEl.resetFields();
      }
    }
  });
};
//返回
const Return = () => {
  const index = localStorage.getItem("index");
  router.push(index as RouteLocationRaw);
};
</script>

<style lang="scss">
.ModifyPassword {
  position: relative;
  width: 1150px;
  margin: auto;
  padding: 20px;
  box-sizing: border-box;
  background-color: white;
  min-height: 80vh;
  .header {
    @include flex(none, center, none);

    .line {
      width: 6px;
      height: 20px;
      background-color: skyblue;
      border-radius: 5px;
      margin-right: 10px;
    }
  }
  .el-form {
    margin-top: 100px;
  }
  .el-form-item__label {
    font-size: 12px;
    color: #999;
  }
  .el-input {
    margin-left: 10px;
    width: 400px;
    height: 40px;
    box-sizing: border-box;
  }
  .btn {
    position: absolute;
    top: 20px;
    left: 140px;
    width: 70px;
    height: 35px;
  }
  .btn1 {
    position: absolute;
    top: 20px;
    left: 220px;
    width: 70px;
    height: 35px;
  }
}
</style>
